import java.io.IOException;

public class BITMAP {
	public static void main(String[] args) throws IOException {
		java.io.BufferedReader r = new java.io.BufferedReader(
				new java.io.InputStreamReader(System.in));

		int number = Integer.parseInt(r.readLine());
		for (int i1 = 0; i1 < number; i1++) {

			String[] input = r.readLine().split("\\s");
			int m = Integer.parseInt(input[0]);
			int n = Integer.parseInt(input[1]);
			int[][] white = new int[m][2];
			int wc = 0;
			String[] a = new String[m];
			for (int i = 0; i < m; i++) {
				a[i] = r.readLine();
				for (int j = 0; j < a[i].length(); j++) {
					if (a[i].charAt(j) == '1') {
						white[wc][0] = i;
						white[wc++][1] = j;
					}
				}
			}

			for (int i = 0; i < m; i++) {

				for (int j = 0; j < n; j++) {
					int d = Integer.MAX_VALUE;
					for (int k = 0; k < wc; k++) {
						int dis = Math.abs(i - white[k][0])
								+ Math.abs(j - white[k][1]);
						if (d > dis) {
							d = dis;
						}
					}
					System.out.print(d + " ");
				}
				System.out.println();
			}
		}

	}

}
